/**
 * 北京中流科技有限公司 版权所有
 * http://www.chinasoa.com.cn
 */
package cn.com.chinasoa.course.subscribe;

import java.io.IOException;

import javax.persistence.EntityManager;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;

import cn.com.chinasoa.course.subscribe.model.ArrangedCourse;
import cn.com.chinasoa.course.subscribe.model.PrivilegeStrategy;
import cn.com.chinasoa.course.subscribe.model.SubscribeOrder;
import cn.com.chinasoa.course.subscribe.util.CommonFieldValidator;
import cn.com.chinasoa.course.subscribe.util.CommonFiledChecker;
import cn.com.chinasoa.course.subscribe.util.DataTypeUtil;
import cn.com.chinasoa.course.subscribe.util.EntityManagerHelper;

import com.x4ee.soa.portal.framework.core.ActionConfig;
import com.x4ee.soa.portal.framework.core.ActionFilter;
import com.x4ee.soa.portal.framework.core.BaseRequestFilter;

/**
 * @author 刘洋(liuyang@chinasoa.com.cn)
 * 2007-11-5上午03:43:06
 */
public class SubscribeOrderEditActionFilter  extends BaseRequestFilter
		implements ActionFilter {

	private static String SUCCESS = "success";
	private static String FAILED = "error";
	
	public static final String ORDER_ID = SubscribeOrderCreateActionFilter.class.getName()+"_ORDER_ID";
	public static final String FORM_ID = SubscribeOrderCreateActionFilter.class.getName()+"_FORM_ID";
	
	public String process(ActionRequest actionRequest, ActionResponse actionResponse,
			ActionConfig actionConfig) throws PortletException, IOException {

		boolean success = false;

		CommonFieldValidator.clearNoticeText(FORM_ID, actionRequest.getPortletSession());
		CommonFiledChecker.clearFieldValue(FORM_ID, actionRequest.getPortletSession());

		String privilegeStrategy = actionRequest.getParameter("privilegeStrategy");

		String orderId = CommonFiledChecker.checkParamValue(FORM_ID, "orderId", actionRequest);
		String studentName = CommonFiledChecker.checkParamValue(FORM_ID, "studentName", actionRequest);
		String studentSex = CommonFiledChecker.checkParamValue(FORM_ID, "studentSex", actionRequest);
		String studentEmail = CommonFiledChecker.checkParamValue(FORM_ID, "studentEmail", actionRequest);
		String studentCompany = CommonFiledChecker.checkParamValue(FORM_ID, "studentCompany", actionRequest);
		String studentPhone = CommonFiledChecker.checkParamValue(FORM_ID, "studentPhone", actionRequest);
		String mobilePhone = CommonFiledChecker.checkParamValue(FORM_ID, "mobilePhone", actionRequest);
		String studentAddress = CommonFiledChecker.checkParamValue(FORM_ID, "studentAddress", actionRequest);
		
		String arrangedCourse = CommonFiledChecker.checkParamValue(FORM_ID, "arrangedCourse", actionRequest);
		
		if((orderId!=null)&&(studentName!=null)&&(studentSex!=null)&&(studentEmail!=null)&&(studentCompany!=null)&&
				(studentAddress!=null)&&(studentPhone!=null)&&(mobilePhone!=null)&&(arrangedCourse!=null)&&
				(!studentName.equals(""))&&(!studentSex.equals(""))&&(!studentEmail.equals(""))&&(!studentCompany.equals(""))&&
				(!studentAddress.equals(""))&&(!studentPhone.equals(""))&&(!mobilePhone.equals(""))&&(!arrangedCourse.equals(""))){
			
			long orderIdLong = DataTypeUtil.parseLong(orderId);
			
			EntityManager em = EntityManagerHelper.getEntityManager();
			em.getTransaction().begin();

			SubscribeOrder vSubscribeOrder = em.find(SubscribeOrder.class, orderIdLong);
			if(vSubscribeOrder!=null){
				vSubscribeOrder.setStudentName(studentName);
				vSubscribeOrder.setStudentEmail(studentEmail);
				vSubscribeOrder.setStudentCompany(studentCompany);
				vSubscribeOrder.setStudentAddress(studentAddress);
				vSubscribeOrder.setStudentPhone(studentPhone);
				vSubscribeOrder.setStudentSex(DataTypeUtil.parseBoolean(studentSex));
				vSubscribeOrder.setMobilePhone(mobilePhone);
				
				ArrangedCourse tArrangedCourse = em.find(ArrangedCourse.class, DataTypeUtil.parseLong(arrangedCourse));
				vSubscribeOrder.setArrangedCourse(tArrangedCourse);
	
				if(privilegeStrategy!=null){
					PrivilegeStrategy tPrivilegeStrategy = em.find(PrivilegeStrategy.class, DataTypeUtil.parseLong(privilegeStrategy));
					vSubscribeOrder.setPrivilegeStrategy (tPrivilegeStrategy);
				}
			}
			em.getTransaction().commit();
			EntityManagerHelper.closeEntityManager();

			SubscribeOrderCreateActionFilter.setOrderId(orderIdLong, actionRequest);

			CommonFieldValidator.clearNoticeText(FORM_ID, actionRequest.getPortletSession());
			success = true;
		}else{
			success = false;
		}

		if(success)return SUCCESS;
		else return FAILED;
	}
}
